<?php
/**
 * Date: 10/21/12
 * Time: 11:31 PM
 * User: Amitav Roy
 */
class User_m extends CI_Model {
	// Declaring the table name for users
  protected $table_name;

	function __construct() {
		parent::__construct();

		// User table name
		$this->table_name = 'users';
	}

	/**
	 * This function will be used to authenticate if the user credentials
	 * are correct or not and will generate the session accordingly.
	 */
	public function user_auth() {
		$username = $this->input->post('username');
		$password = hash('sha512',$this->input->post('password'));

		$this->db->select();
		$this->db->from($this->table_name);
		$this->db->where('username',$username);
		$this->db->where('password',$password);
		$query = $this->db->get();

		$row_count = $query->num_rows();

		if ($row_count == 1) {
			// Passing to set the session data.
			$this->_generate_user_session($query->result());

			// Once done, redirect to Dashboard.
			redirect('dashboard/index');
		}
		else {
      // If user comes to this page in a way other than login, he should be redirected.
			redirect('user/login');
		}
	}

	/**
	 * This function will generate the user session which will be user commonly.
	 */
	protected function _generate_user_session($user_data) {
		// Adding the required details in the session array.
		$session_data['auth'] = 1;
		$session_data['username'] = $user_data[0]->username;
		$session_data['displayname'] = $user_data[0]->display_name;
    $session_data['uid'] = $user_data[0]->uid;

		// Setting the data array in session.
		$this->session->set_userdata($session_data);

		return true;
	}
}

/* End of file user_m.php */
/* Location: ./application/controllers/user_m.php */